import { queryCartList } from "@/service/api/sp-store/commodity.js";
import {
  getShopDetailNoload,
  getIndustry,
  getIndustrys,
} from "@/service/api/sp-store/index.js";
import { myTool } from "@/utils/tool.js";
import { appGetStorage } from "@/utils/storage";
import { TABLE_QUERY_KEY } from "@/pages/sp-store/const";
const state = {
  dineParams: "",
  cartData: "",
  cartNum: 0,
  pubShopId: "",
  cartPrice: 0,
  shopStatus: 0,
  industryType: {},
  distributionType: {},
  pubShopInfo: {},
  lowCompatibility:true,
  orderData:{}
};

const mutations = {
  SET_DINE: (state, val) => {
    state.dineParams = val;
  },
  SET_PUBSHOP_ID: (state, val) => {
    state.pubShopId = val;
  },
  SET_CARTDATA: (state, val) => {
    state.cartData = val;
  },
  SET_CARTNUM: (state, val) => {
    state.cartNum = val;
  },
  SET_CARTPRICE: (state, val) => {
    state.cartPrice = val;
  },
  SET_SHOP_STATUS: (state, val) => {
    state.shopStatus = val;
  },
  SET_INDUSTRY_TYPE: (state, val) => {
    state.industryType = val;
  },
  SET_DISTRIBUTION_TYPE: (state, val) => {
    state.distributionType = val;
  },
  SET_PUBSHOP_INFO: (state, val) => {
    state.pubShopInfo = val;
  },
  SET_LOW_COMPATIBILITY: (state, val) => {
    state.lowCompatibility = val
  },
  SET_ORDER_DATA:(state,val)=>{
    state.orderData = val
  }
};
const actions = {
  async initCart({ commit, state, dispatch }, data) {
    let pubShopId = "";
    if (data || state.pubShopId) {
      if (data) {
        pubShopId = data;
        commit("SET_PUBSHOP_ID", pubShopId);
      } else {
        pubShopId = state.pubShopId;
      }
      queryCartList({
        pubShopId,
      }).then((res) => {
        commit("SET_CARTDATA", res);
        let { items, cartNum = 0, cartPrice = 0 } = res;
        if (items) {
          items.forEach((element) => {
            if (!element.errorCode || element.errorCode == null) {
              cartNum += element.num;
              cartPrice += element.num * element.price;
            }
          });
        }
        commit("SET_CARTNUM", cartNum);
        commit("SET_CARTPRICE", myTool.toFixedNum(cartPrice / 100));
        // dispatch("initShop");
        dispatch("getIndustrys");

      });
    }
  },
  // async initShop({ commit, state, dispatch }) {
  //   getShopDetailNoload({
  //     id: state.pubShopId,
  //   }).then((res) => {
  //     commit("SET_SHOP_STATUS", res.allStatus);
  //     commit("SET_PUBSHOP_INFO", res);
  //     dispatch("getIndustrys");
  //   });
  // },
  getIndustry({ commit, state }) {
    getIndustry().then((res) => {
      commit("SET_INDUSTRY_TYPE", res);
    });
  },
  getIndustrys({ commit, state }) {
    let { pubShopId } = state;
    const tableParams = appGetStorage(TABLE_QUERY_KEY)
      ? JSON.parse(appGetStorage(TABLE_QUERY_KEY))
      : {};
    let params = {
      pubShopId,
    };
    if (tableParams?.tableNo) {
      params.tableNo = tableParams?.tableNo;
    }
    getIndustrys(params).then((res) => {
      commit("SET_DISTRIBUTION_TYPE", res);
    });
  },
};
const getters = {};

export default {
  namespaced: true,
  actions,
  state,
  getters,
  mutations,
};
